// pages/index/dealLog/dealLog.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dateGroup:[],
    pageNum:1,
    pageSize:10,
    height:wx.getSystemInfoSync().windowHeight
  },
  onLoad:function(){
    //调用接口获取新闻列表
    this.getData()
  },
  //网络请求，获取数据
  getData(){
    app.$http.get('/app/deallog/list', {pageNum:this.data.pageNum,pageSize:this.data.pageSize,username:wx.getStorageSync('username')}).then(res=>{
      let dataList = []
      for(var i=0;i<res.data.records.length;i++){
        var item = res.data.records[i]
        if(dataList.length == 0){
          dataList.push({name:this.getYearMonth(item.logTime),list:[item],money:item.money})
          continue
        }
        if(dataList[dataList.length-1].name === this.getYearMonth(item.logTime)){
          dataList[dataList.length-1].list.push(item)
          dataList[dataList.length-1].money += item.money
        }else{
          dataList.push({name:this.getYearMonth(item.logTime),list:[item],money:item.money})
          continue
        }
      }
      this.setData({
        dateGroup:dataList
      })
    }).catch(err => {
      console.log(err)
    })   
  },
  //网络请求，添加数据
  appendData(){
    app.$http.get('/app/deallog/list', {pageNum:this.data.pageNum+1,pageSize:this.data.pageSize,username:wx.getStorageSync('username')}).then(res=>{
      let dataList = this.data.dateGroup
      for(var i=0;i<res.data.records.length;i++){
        var item = res.data.records[i]
        if(dataList.length == 0){
          dataList.push({name:this.getYearMonth(item.logTime),list:[item],money:item.money})
          continue
        }
        if(dataList[dataList.length-1].name === this.getYearMonth(item.logTime)){
          dataList[dataList.length-1].list.push(item)
          dataList[dataList.length-1].money += item.money
        }else{
          dataList.push({name:this.getYearMonth(item.logTime),list:[item],money:item.money})
          continue
        }
      }
      this.setData({
        dateGroup:dataList,
        pageNum:this.data.pageNum+1
      })
    }).catch(err => {
      console.log(err)
    })   
  },
  //滑动到底部
  lower(){
    this.appendData()
    console.log('滑动到底部')
  },
  //获取年月分组
  getYearMonth(dateStr){
    var result = dateStr.split("-")[0]
    result +=  "-" + dateStr.split("-")[1]
    return result
  }
})